﻿#region Using Declarations

using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using Portfolio.Models;

#endregion

namespace Portfolio.Persistencia
{
    public class Categorias
    {
        #region Methods

        public static Categoria BuscarCategoria(int id)
        {
            Categoria categoria = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = string.Format("SELECT * FROM Categorias WHERE Id = {0}", id)
                };

                conn.Open();

                try
                {
                    var reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            categoria = new Categoria()
                            {
                                Id = (int)reader["Id"],
                                Nome = reader["Nome"].ToString(),
                                NomeExibicao = reader["NomeExibicao"].ToString()
                            };
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return categoria;
        }

        public static IEnumerable<Categoria> ListarCategorias()
        {
            List<Categoria> categorias = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandText = "SELECT * FROM Categorias",
                    CommandType = System.Data.CommandType.Text
                };

                conn.Open();

                try
                {
                    var reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        categorias = new List<Categoria>();

                        while (reader.Read())
                        {
                            categorias.Add(new Categoria()
                            {
                                Id = (int)reader["Id"],
                                Nome = reader["Nome"].ToString(),
                                NomeExibicao = reader["NomeExibicao"].ToString()
                            });
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return categorias;
        }

        #endregion
    }
}